#ifndef _PRECISIONTIMER_H_
#define _PRECISIONTIMER_H_

#include <windows.h>
class CPrecisionTimer{
protected:
	LARGE_INTEGER ldFreq, lStart, lEnd,lRandom; 
	double dFreq;
	double elapsedTime;
	
public:
	int Fps;
	CPrecisionTimer(){
		QueryPerformanceFrequency(&ldFreq);
		dFreq = 1.0/(double)ldFreq.QuadPart;
		elapsedTime=0.0;
		Fps=0;
	}
	inline void Start(){
		QueryPerformanceCounter(&lStart);
		//elapsedTime=0.0;
	}
	inline double Stop()  {
		// Return duration in seconds...
		QueryPerformanceCounter(&lEnd);
		//elapsedTime=double(lEnd.QuadPart - lStart.QuadPart) / ldFreq.QuadPart;
		return (double(lEnd.QuadPart - lStart.QuadPart) / ldFreq.QuadPart);
	}
	inline int Random(float seed){
		seed*=1000.0f;
		int returnValue;
		int sign=1;
		QueryPerformanceCounter(&lRandom);
		if(lRandom.LowPart%2) sign*=-1;
		returnValue = lRandom.QuadPart % (int)seed;
		returnValue/=1000;
		return sign*returnValue;
	}
	/*
	inline void Pause(){
		elapsedTime+=QueryPerformanceCounter(&lEnd);
		elapsedTime+=double(lEnd.QuadPart - lStart.QuadPart) / ldFreq.QuadPart;
	}
	inline void Continue(){
		QueryPerformanceCounter(&lStart);
	}
	*/

};
class FrameTimer:public CPrecisionTimer{
	double dFreq;
public:
	double refreshTime;
	double frameNumber;
	double frameCount;
	double framesPerSecond;
	double lastFrameDuration;
	double averageTime;
	FrameTimer(double time=2.0){
		refreshTime=time;
		frameNumber=0.0f;
		frameCount=0.0f;
		lastFrameDuration=0.0f;
		framesPerSecond=0.0f;
		averageTime = 0.0f;
		QueryPerformanceFrequency(&ldFreq);
		dFreq = 1.0/(double)ldFreq.QuadPart;
	}
	inline bool RefreshTimeElapsed(){
		return (averageTime>refreshTime)?true:false;
	}
	inline void Update(){
		framesPerSecond = frameCount/averageTime;
		frameCount=0.0f;
		averageTime=0.0f;
	}
	inline void Start(){
		QueryPerformanceCounter(&lStart);
	}
  
	inline double Stop()  {
		// Return duration in seconds...
		QueryPerformanceCounter(&lEnd);
		frameNumber++;
		frameCount++;
		lastFrameDuration = (double(lEnd.QuadPart - lStart.QuadPart) * dFreq);
		averageTime+=lastFrameDuration;
		//framesPerSecond = lastFrameDuration * 0.01666;
		return lastFrameDuration;
	}
};

#endif // _PRECISIONTIMER_H_